package bll

import (
	"sort"

	"7qule.com/sd3/CrossServer/src/bll/challenge/dal"
	. "7qule.com/sd3/CrossServer/src/bll/challenge/model"
	"7qule.com/sd3/CrossServer/src/bll/world"
)

// 擂台阶段Model务逻辑类
type baseChallengeStageModelBll struct {
	// 擂台阶段Model数据
	data map[int]*BaseChallengeStageModel
}

// --------------------------------初始化--------------------------------------------------

// 擂台阶段Model逻辑类
var BaseChallengeStageModelBll = &baseChallengeStageModelBll{}

// 初始化
func init() {
	// 注册数据初始化函数
	world.LoadMgr.RegisterFunc(BaseChallengeStageModelBll.StructName(), BaseChallengeStageModelBll.initData)
}

// StructName 模块名
func (this *baseChallengeStageModelBll) StructName() string {
	return "BaseChallengeStageModelBll"
}

// initData 初始化数据
// 返回值:
// err:错误Model
func (this *baseChallengeStageModelBll) initData() (errList []error) {
	tempList, err := dal.BaseChallengeStageModelDal.GetData()
	if err != nil {
		errList = append(errList, err)
		return
	}

	tempMap := make(map[int]*BaseChallengeStageModel)

	for _, item := range tempList {
		tempMap[item.StageId] = item
	}

	this.data = tempMap

	return
}

// --------------------------------生成的基础方法--------------------------------------------------

//
// GetData
// @Description:获取数据
// @receiver this
// @return map[int]*BaseChallengeStageModel:擂台数据
//
func (this *baseChallengeStageModelBll) GetData() map[int]*BaseChallengeStageModel {
	return this.data
}

// --------------------------------内部方法--------------------------------------------------

//
// getStageSort
// @Description:页签排序
// @receiver this
// @return []int:页签id
//
func (this *baseChallengeStageModelBll) getStageSort() []int {

	keys := make([]int, 0)
	for k := range this.data {
		keys = append(keys, k)
	}
	sort.Ints(keys)
	return keys
}

// --------------------------------外部方法--------------------------------------------------

//
// GetSmallestStage
// @Description:获取最小页签
// @receiver this
// @return int:页签id
//
func (this *baseChallengeStageModelBll) GetSmallestStage() int {
	return this.getStageSort()[0]
}

//
// GetMaxStage
// @Description:获取最大页签
// @receiver this
// @return int:页签id
//
func (this *baseChallengeStageModelBll) GetMaxStage() int {
	keys := this.getStageSort()
	if len(keys) < 0 {
		return len(keys)
	}
	return keys[len(keys)-1]
}
